<template>
  <div class="container">
    <el-row class="">
      <el-col :span="20" align="left">
        <el-input
          clearable
          v-model="search.processDefName"
          placeholder="流程信息"
          style="width: 120px; margin-right: 15px"
        >
        </el-input>

        <el-input
          clearable
          v-model="search.taskName"
          placeholder="当前环节"
          style="width: 120px; margin-right: 15px"
        >
        </el-input>

        <el-button class="ordinary" @click="getTableList">搜索</el-button>
      </el-col>
      <el-col :span="4" align="right">
        <el-button class="ordinary" @click="back">返回</el-button>
      </el-col>
    </el-row>
    <div>
      <Table
        :loading="isLoading"
        :tableArr="tableDate"
        :pagination="pagination"
        :columns="columns"
        :width="100"
        :pageSize="pagination.limit"
        :showSize="true"
        :isCheckbox="true"
        :total="pagination.total"
        @currentChange="currentChange"
      >
        <template #operate="{ row }" width="100">
          <el-button v-if="row.taskId!='0'" size="small" class="ordinary" @click="handleEdit(row)"
            >审核</el-button
          >
          <el-button v-if="row.taskId=='0'" size="small" class="ordinary" @click="handleReport(row)"
            >上报</el-button
          >
        </template>
      </Table>
    </div>
    <el-dialog v-model="reportDialog" width="60%" title="审查上报申请" @close="handleClose">
      <Information title="审查上报申请" ref="reportRef" v-if="reportDialog"></Information>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="reportDialog = false">关闭</el-button>
          <el-button type="primary" @click="handleReportSave()">提交</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      v-model="dealVisible"
      title="系统正在处理中....."
      width="40%"
      top="14%"
      height="400"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-alert
        type="success"
        :closable="false"
        style="margin-bottom: 20px"
        description="当前流程正在处理中,请稍等……"
      >
      </el-alert>
    </el-dialog>
  </div>
</template>
<script>
// import SelectOption from '@/views/science/components/selectOption/index.vue'
import Table from '@/views/science/components/table/table.vue'
import moment from 'moment'
import Information from '@/views/project/review-acceptance/progress-examine/components/information.vue'
import { ElMessageBox, ElMessage } from 'element-plus'
import { servicesLoading } from '@/utils/loading'
import { getTable } from '../science/api/homePage'
import { startProcess } from '@/views/project/api/commom'
import { examineReportSave, getDetail, dealedReport } from '@/views/project/api/examine'
import { onMounted, reactive, toRefs, ref } from 'vue'
import tableHead from './dealHeader.json'
import { useStore } from 'vuex'

export default {
  components: {
    Information,
    Table,
  },
  setup(props, { emit }) {
    // 发送请求
    onMounted(() => {
      // 获取数据字典
      // getCodeList();
      getTableList()
    })
    const reportRef = ref()
    const store = useStore()

    let data = reactive({
      id: '',
      flag: 1,
      infoDialog: false,
      reportId: '',
      toDealId: '',
      reportDialog: false,
      dealVisible: false,
      ids: [],
      pagination: { page: 1, limit: 10, total: 0 }, //分页
      isLoading: false,
      unitNameList: [],
      awardList: [],
      tableDate: [],
      columns: tableHead.dealHeader,
      search: {
        processDefName: '',
        taskName: '',
      },
    })

    // 获取列表信息
    let getTableList = () => {
      ;(data.isLoading = true),
        getTable({
          page: data.pagination.page,
          size: data.pagination.limit,
          ...data.search,
        })
          .then((res) => {
            let resData = res.data.data.records
            resData.map((item) => {
              if (item) {
                item.createTime = moment(item.createTime).format('YYYY-MM-DD')
              }
            })
            data.tableDate = res.data.data.records
            data.pagination.total = res.data.data.total
            data.isLoading = false
          })
          .catch((err) => {
            data.isLoading = false
          })
    }

    // 分页

    let currentChange = (current) => {
      data.pagination.page = current

      getTableList()
    }

    // 编辑

    let handleEdit = (row) => {
      emit('changeProcess', row, '0')
    }
    const handleReport = (row) => {
      console.log(row,'row');
      store.state.projectCenter.reportDetailForm.releaseId = row.businessId
      data.reportId = row.businessId
      // data.toDealId = row.id
      data.reportDialog = true
      console.log(data.reportId, 'data.reportId')
    }
    const handleReportSave = () => {
      reportRef.value.submitForm().then(() => {
        ElMessageBox.confirm('确认上报审查并提交申请吗？', '提示', {
          confirmButtonText: '确认',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          examineReportSave(store.state.projectCenter.reportDetailForm).then((res) => {
            if (res.data.code == 200) {
              data.reportDialog = false
              dealedReport(data.reportId).then((res) => {
                console.log(res, 'dealedReport')
              })
              const loading = servicesLoading({ fullscreen: true })
              startProcess({
                flowKey: 'Process-Review',
                businessKey: store.state.projectCenter.reportDetailForm.releaseId,
                businessType: 'YW08',
                random: new Date().valueOf(),
              })
                .then((res) => {
                  loading.close()
                  if (res.data.code != 200) {
                    ElMessage({
                      message: '流程启动失败,请稍后重试',
                      type: 'warning',
                    })
                  } else {
                    data.dealVisible = true
                    deal(store.state.projectCenter.reportDetailForm.releaseId, 1)
                  }
                })
                .catch(() => {
                  loading.close()
                })
              // getTableList()
            }
          })
        })
      })
    }
    // 处理不通过,提交的过程中.......
    let deal = (id, state) => {
      let times = setInterval(() => {
        getDetail(data.reportId).then((res) => {
          const resData = res.data.data
          if (resData.status == state || (state == 2 && resData.status == 1)) {
            data.dealVisible = false
            ElMessage({
              message: '流程办理成功',
              type: 'success',
            })
            clearTimeout(times)
            getTableList()
          }
        })
      }, 3000)
    }
    let back = () => {
      emit('changeBoard')
    }

    return {
      ...toRefs(data),
      handleEdit,
      handleReport,
      handleReportSave,
      back,
      reportRef,
      getTableList,
      currentChange,
    }
  },
}
</script>
<style lang="scss">
.conUnitName {
  display: inline-block;
  width: 200px;
}

.container {
  background-color: white !important;
  padding: 20px;
  width: 100%;
  height: 100%;
}

.gy-cascader {
  .el-cascader-node {
    max-width: 250px;
  }
}
</style>
